<?php
/**
 * Created by PhpStorm.
 * User: yangsen
 * Date: 15/7/6
 * Time: 上午11:08
 */

require_once 'AdminParent.php';
class Admin extends AdminParent {


    const PERPAGE = 10;

    public function login() {
        $this->load->view('admin/login');
    }

    public function index() {
        $this->main();
    }

    public function loginSubmit() {
        $u = $this->input->post('username');
        $p = $this->input->post('password');
        if($u=='admin' && $p=='zuanQAZXSW') {
            $_SESSION['admin']=array('name'=>'系统管理员','admin');
            redirect('admin/main');
        } else {
            redirect('admin/login');
        }
    }

    public function main() {
        $this->checklogin();
        $this->load->view('admin/admin_header');
        $this->load->view('admin/adminmain');
    }


    public function statusticsPage($offset=0,$Ymd=0) {
        $now = date('YmdH', time());
        $cond='1 =1 ';
        if(!empty($Ymd)) {
            $queryDay=str_replace('-','',$Ymd);
            $cond ='ctimehour >'.$queryDay.'00 and ctimehour <'.$queryDay.'24';
        } else {
            $Ymd='';
        }
        $data['Ymd'] = $Ymd;

        $this->checklogin();
        //读取PV数据,新增用户数据,支付了钱的人数统计,支付的金额统计
        $this->load->view('admin/admin_header');
        $this->load->library('pagination');

        //读取结果数据

        $stattable = $this->db->order_by('ctimehour','desc')->limit($offset,Admin::PERPAGE)->get_where('t_stat',$cond)->result_array();
        $rowcnts = $this->db->count_all('t_stat');
        $data['stattable'] = $stattable;

        $pageconf['base_url'] = 'admin/statusticsPage';
        $pageconf['total_rows'] = $rowcnts;
        $pageconf = array_merge($this->config->item('pagination'),$pageconf);

        $this->pagination->initialize($pageconf);
        $data['pagenation'] = $this->pagination->create_links();
        $this->load->view('admin/statResult',$data);
    }

    public function userlist($offset=0) {
        $now = date('YmdH', time());

        $this->checklogin();
        //读取PV数据,新增用户数据,支付了钱的人数统计,支付的金额统计
        $this->load->view('admin/admin_header');
        $this->load->library('pagination');
        //读取结果数据

        $userlist = $this->db->order_by('ctime','desc')->limit($offset,Admin::PERPAGE)->get_where('t_user')->result_array();
        $rowcnts = $this->db->count_all('t_user');
        $data['userlist'] = $userlist;

        $pageconf['base_url'] = 'admin/userlist';
        $pageconf['total_rows'] = $rowcnts;
        $pageconf = array_merge($this->config->item('pagination'),$pageconf);

        $this->pagination->initialize($pageconf);
        $data['pagenation'] = $this->pagination->create_links();
        $this->load->view('admin/mgrUser',$data);
    }

    public function progress($offset=0,$range=0) {
        $arr_range = array();

        $this->checklogin();

        $cond_arr = array('p.status'=>2);//支付成功回调后的status=2.

        //统计到达各个阶段的用户
        $this->load->view('admin/admin_header');
        $this->load->library('pagination');

        //读取结果数据
        $this->db->select('sum(fee) as total_pay,p.ctime,u.nickname,u.headimgurl,u.realname,u.sex,u.mobile');
        $this->db->join('t_user u','u.openid=p.owner_openid')->group_by('u.openid');
        if($range!==0) {
                $arr_range = explode('_',$range);
            $this->db->having(array('total_pay >',$arr_range[0]));
            $this->db->having(array('total_pay <=',$arr_range[1]));

        }
        $result = $this->db->limit($offset,ADMIN::PERPAGE)->get_where('t_payrecord p',$cond_arr)->result_array();

        //分页行数总和
        $recordcnt = $this->db->query('select count(*) cnt from (select owner_openid from t_payrecord group by owner_openid) b')->result_array();
        $recordcnt = $recordcnt[0]['cnt'];
        $pageconf['base_url'] = 'admin/userlist';
        $pageconf['total_rows'] =$recordcnt ;
        $pageconf = array_merge($this->config->item('pagination'),$pageconf);

        $this->pagination->initialize($pageconf);

        //$this->db->select();
        $data['result']=$result;
        $data['pagenation'] = $this->pagination->create_links();

        $this->load->view('admin/progress',$data);
    }

    public function logout() {
        unset($_SESSION['admin']);
        redirect('admin/login');
    }

    public function checklogin() {
        if(!isset($_SESSION['admin'])) {
            redirect('admin/login');
        }
    }
}